package org.zyj.io.example.case9;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

class CPClient {

    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        try {
            final Bootstrap bootstrap = new Bootstrap();
            //
            bootstrap.group(eventLoopGroup)//
                    .channel(NioSocketChannel.class)//
                    .handler(new ChannelInitializer<SocketChannel>() { //
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline()//
                                    .addLast(new CustomDecoder())
                                    .addLast(new CustomEncoder())
                                    .addLast(new ClientHandler());
                        }
                    });
            System.out.println("client setup done");

            final ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 9123).sync();//

            channelFuture.channel().closeFuture().sync();
        } finally {
            eventLoopGroup.shutdownGracefully();
        }
    }
}
